package com.san.dissertation.dao.repository.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.WriteResult;
import com.san.dissertation.bpm.model.Process;
import com.san.dissertation.dao.repository.ProcessRepostitoryCustom;

public class ProcessRepositoryImpl implements ProcessRepostitoryCustom {

	@Autowired
	private MongoTemplate mongoTemplate;

	/*
	 * public List<Process> searchByCriteria(ProductSearchCriteria criteria) {
	 * Query query = new Query(); if ( StringUtils.hasText(criteria.getSku())) {
	 * Criteria c = Criteria.where("sku").is(criteria.getSku());
	 * query.addCriteria(c); } if (StringUtils.hasText(criteria.getTitle())) {
	 * Criteria c = Criteria.where("title").regex(".*" + criteria.getTitle() +
	 * ".*", "i"); query.addCriteria(c); } if
	 * (StringUtils.hasText(criteria.getDescription())) { Criteria c =
	 * Criteria.where("description").regex(".*" + criteria.getDescription() +
	 * ".*", "i"); query.addCriteria(c); } if
	 * (StringUtils.hasText(criteria.getProductType())) { Criteria c =
	 * Criteria.where("type").is(criteria.getProductType());
	 * query.addCriteria(c);
	 * 
	 * } if (StringUtils.hasText(criteria.getTrack())) { Criteria c =
	 * Criteria.where("details.tracks").regex(".*" + criteria.getTrack() + ".*",
	 * "i"); query.addCriteria(c); } if
	 * (StringUtils.hasText(criteria.getChapter())) { Criteria c =
	 * Criteria.where("details.chapters").regex(".*" + criteria.getChapter() +
	 * ".*", "i"); query.addCriteria(c); } return mongoTemplate.find(query,
	 * Product.class); }
	 */

	public List<Process> searchByCriteria() {
		// TODO Auto-generated method stub

		return null;
	}

	public void updateTaskEndById(String instanceId, String taskId) {

		/*
		 * Criteria instanceCriteria=Criteria.where("instance").is(instanceId);
		 * 
		 * BasicQuery query= new
		 * BasicQuery(instanceCriteria.getCriteriaObject());
		 * 
		 * Task task= new Task(); task.setEndDate(new Date());
		 */

		/*
		 * Query query= new Query();
		 * query.addCriteria(Criteria.where("key").is("sankarMakerChecker"));
		 * //query.fields().include("path");
		 * 
		 * Update update= new Update(); update.
		 * 
		 * 
		 * System.out.println(mongoTemplate.find(query, Process.class));
		 * 
		 * 
		 * mongoTemplate.updateFirst(query, update, ProcessPath.class);
		 * 
		 * 
		 * mongoTemplate.updateFirst(query, new Update().set("instance.$", new
		 * Date()), Process.class);
		 */

		Query query = new Query(Criteria.where("key").is("sankarMakerChecker")
				.and("path")
				.elemMatch(Criteria.where("name").is("sankarMakerChecker:Path:3")));

		Update update = new Update().set("path.$.name",
				"chk");
		
		mongoTemplate.updateFirst(query, update, Process.class);


	}

}
